package com.prp.db.daoimpl.mysql;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.prp.db.idao.ISubjectRecommendationDAO;
import com.prp.db.po.SubjectRecommendation;

/**
 * A data access object (DAO) providing persistence and search support for
 * SubjectRecommendation entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.prp.db.daoimpl.mysql.SubjectRecommendation
 * @author MyEclipse Persistence Tools
 */

public class SubjectRecommendationDAO extends BaseHibernateDAO implements
		ISubjectRecommendationDAO {
	private static final Log log = LogFactory
			.getLog(SubjectRecommendationDAO.class);
	// property constants
	public static final String CATEGORY_CHILD_ID = "categoryChildId";
	public static final String SUBJECT_TITLE = "subjectTitle";
	public static final String PUBLISH_ID = "publishId";
	public static final String CONTENT = "content";
	public static final String RECOMMEND_ENTITY_ID = "recommendEntityId";
	public static final String CLICK_FREQ = "clickFreq";
	public static final String WAITING_TIME = "waitingTime";
	public static final String REPLY_NUM = "replyNum";

	public void save(SubjectRecommendation transientInstance) {
		log.debug("saving SubjectRecommendation instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(SubjectRecommendation persistentInstance) {
		log.debug("deleting SubjectRecommendation instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SubjectRecommendation findById(java.lang.Integer id) {
		log.debug("getting SubjectRecommendation instance with id: " + id);
		try {
			SubjectRecommendation instance = (SubjectRecommendation) getSession()
					.get("com.prp.db.po.SubjectRecommendation", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SubjectRecommendation instance) {
		log.debug("finding SubjectRecommendation instance by example");
		try {
			List results = getSession().createCriteria(
					"com.prp.db.daoimpl.mysql.SubjectRecommendation").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SubjectRecommendation instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from SubjectRecommendation as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCategoryChildId(Object categoryChildId) {
		return findByProperty(CATEGORY_CHILD_ID, categoryChildId);
	}

	public List findBySubjectTitle(Object subjectTitle) {
		return findByProperty(SUBJECT_TITLE, subjectTitle);
	}

	public List findByPublishId(Object publishId) {
		return findByProperty(PUBLISH_ID, publishId);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByRecommendEntityId(Object recommendEntityId) {
		return findByProperty(RECOMMEND_ENTITY_ID, recommendEntityId);
	}

	public List findByClickFreq(Object clickFreq) {
		return findByProperty(CLICK_FREQ, clickFreq);
	}

	public List findByWaitingTime(Object waitingTime) {
		return findByProperty(WAITING_TIME, waitingTime);
	}

	public List findByReplyNum(Object replyNum) {
		return findByProperty(REPLY_NUM, replyNum);
	}

	public List findAll() {
		log.debug("finding all SubjectRecommendation instances");
		try {
			String queryString = "from SubjectRecommendation";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SubjectRecommendation merge(SubjectRecommendation detachedInstance) {
		log.debug("merging SubjectRecommendation instance");
		try {
			SubjectRecommendation result = (SubjectRecommendation) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SubjectRecommendation instance) {
		log.debug("attaching dirty SubjectRecommendation instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findByKeyword(Object pattern, int start, int amount) {
		log.debug("finding SubjectRecommendation instance with keyword: "
				+ ", value: " + pattern);
		try {
			String queryString = "from SubjectRecommendation as model where model."
					+ "subjectTitle" + " like ?";
			Query queryObject = getSession().createQuery(queryString)
					.setFirstResult(start).setMaxResults(amount);
			queryObject.setParameter(0, pattern);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public void attachClean(SubjectRecommendation instance) {
		log.debug("attaching clean SubjectRecommendation instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}